<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['lang']){
	$_SESSION['show_lang']=$_RQ['lang'];
}
$shopset=$this->plugin_setting('shopset');
//检查异常订阅
if($_SESSION['subscribeid']){
	if(function_exists('uperror_publish')){
		uperror_publish($_SESSION['subscribeid']);
	}
}
if($_RQ['op']=='main'){
	//执行任务
	$this->model('task')->orderup();
	$this->model('task')->int_comments();
	//未登录下单
	$return['ischecklogin']=$this->model('member')->checklogin('','','','',true);
	$return['show_setmobile']=cfc('core')->check_setmobile(2);
	$return['show_setmember']=cfc('core')->check_setmember(2);
	$return=$this->model('page/order_list')->getmain($return);
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>0,'userdeleted'=>0);
	if($_SESSION['uid']){
		$params['mid']=$_SESSION['uid'];
	}elseif($_RQ['mobile']){
		$params['mobile']=trim($_RQ['mobile']);
		$params['mid']=0;
	}else{
		$params['deleted']='nodata';
	}
	if(isset($_RQ['status']) && $_RQ['status'] !=100){
		$params['status']=intval($_RQ['status']);
	}
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=intval($_RQ['ordersn']);
	}
	if(!empty($_RQ['userdeleted'])){
		$params['userdeleted']=1;
	}
	if(!empty($_RQ['title'])){
		$return['params']['title']=$_RQ['title'];
		$goodsparams=array('uniacid'=>$_SESSION['uniacid']);
		$goodsparams['title like']='%'.trim($_RQ['title']).'%';
		$goodsids=pdo_getall('jy_weishop_goods',$goodsparams,'id','id');
		if($goodsids){
			$goodsids=array_keys($goodsids);
			$ids=pdo_getall('jy_weishop_order_goods',array('goodsid'=>$goodsids,'uniacid'=>$_SESSION['uniacid']),'orderid','orderid');
			if($ids){
				$ids=array_keys($ids);
				$params['id']=$ids;
			}else{
				$params['id']=0;
			}
		}else{
			$params['id']=0;
		}
	}
	if(!$params['plugin_name']){
		$params['plugin_name <>']='jy_newcar';
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','price','addprice','ispayadd','createtime','ispay','mid','paytime','status','buytype','sendtype','ordersn','addressid','dispatchprice','yuejiemid','paytype','pay_id','creditfee','delcredit1');
	$list=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	$feemoneyset=$this->plugin_setting('feemoney');
	$feemoneyset['feerade']=$feemoneyset['feerade']?:5;
	$pay_types=get_pay_types();
	foreach($list as &$item){
		$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),array('id','status','goodsid','istaocan','optionid','price','total','goodssn','productsn','weight'));
		$merchs =array();
		$item['feemoney']=0;
		foreach($ordergoods as &$gv){
			$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice','unit','image','title','merchid'));
			$goodsinfo['ordergoodsid']=$gv['id'];
			$goodsinfo['refund_id']=$this->pdo_getcolumn('order_refund',array('ordersn'=>$item['ordersn'],'ordergoodsid'=>$gv['id']),'id');
			$goodsinfo['can_refund']=$this->model('order/refund')->check_order_rerefund($item['id'],'',$gv['goodsid'],$gv['id']);
			$goodsinfo['total']=$gv['total'];
			$goodsinfo['price']=$gv['price'];
			if($_SESSION['is_tomedia']){
				$goodsinfo['image']=$goodsinfo['image']?tomedia($goodsinfo['image']):'';
			}
			$goodsinfo['weight']=$gv['weight']>0?$gv['weight']:pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),'weight');
			if($feemoneyset['isopen']==1){
				$gv['feemoney']=$goodsinfo['marketprice']>0?$goodsinfo['marketprice']-$item['price']:$feemoneyset['feerade']*$item['price']/100;
				$gv['feemoney']=price($gv['total']*$gv['feemoney'],'int');
				$item['feemoney'] +=$gv['feemoney'];
				$goodsinfo['feemoney']=price($gv['feemoney']);
			}
			//获取门店
			$stores = array();
			$goodsinfo['storeids'] = json_decode($goodsinfo['storeids'],true);
			if ($goodsinfo['isverify'] == 1) {
				if (empty($goodsinfo['storeids'])) {
					$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3)));
				}else {
					foreach($goodsinfo['storeids'] as $sv){
						$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3),'id'=>$sv['id']));
					}
				}
			}elseif($shopset['openstore'] ==1){
				if (empty($goodsinfo['storeids'])) {
					$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
				}else {
					foreach($goodsinfo['storeids'] as $sv){
						$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$sv['id']));
					}
				}
			}
			$goodsinfo['store']=$stores;
			//商品规格
			if($gv['optionid']){
				$options=pdo_get('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'id'=>$gv['optionid']),array('title','specs','weight'));
				if($gv['istaocan']){
					$goodsinfo['selespename']=$options['title'];
				}else{
					$specs=$options['specs'];
					$specs=explode("_",$specs);
					foreach($specs as &$spec){
						$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
					}
					$goodsinfo['selespename']=implode("，",$specs);
					$goodsinfo['weight']=$options['weight'];
				}
			}
			//获取商户
			if($goodsinfo['merchid']){
				$merchinfo = pdo_get('jy_merch_user',array('id'=>$goodsinfo['merchid'],'uniacid'=>$_SESSION['uniacid']));
				$merchs[$goodsinfo['merchid']] = $merchinfo;
				$merchs[$goodsinfo['merchid']]['goods'][] = $goodsinfo;
			}else{
				$merchs[0]['goods'][] = $goodsinfo;
			}
		}
		$item['feemoney']=price($item['feemoney'],'int');
		//获取地址
		$item['address']=json_decode($item['address'],true);
		if($item['address'] && $item['address']['name']){	
			$address=$item['address'];
		}else{
			$address=$item['addressid'];
		}
		$item['address']=model('core/member')->get_address_str($address);
		$item['pingjiatime'] = pdo_getcolumn('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id'],'commentid'=>0),'createtime');
		if($item['pingjiatime']){
			$comment_changedays=$shopset['comment_changedays']?:0;
			$endpingjiatime = $item['pingjiatime'] + $comment_changedays*24*3600;
			if($endpingjiatime>TIMESTAMP){
				$item['pingjia']=1;//1修改评价；2追加评价
			}else{
				$zuipingjia = pdo_count('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id'],'commentid >'=>0),'id');
				if($zuipingjia<1){
					$item['pingjia']=2;
				}else{
					$item['pingjia']=3;
				}
			}
		}
		$item['merchs']=$merchs;
		$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		$item['statustitle']=$this->order_status[$item['status']];
		$item['buytypetitle']=$this->buy_types[$item['buytype']];
		$item['paytypetitle']=$pay_types[$item['paytype']];
		if($item['paytype']=='credit1'){
			$item['creditfee']=$item['delcredit1'];
			$item['price']=$item['delcredit1'];
		}
		$item['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'total');
		$item['can_refund']=$this->model('order/refund')->check_order_rerefund($item['id']);
		$item['can_cancel']=$this->model('order/refund')->check_order_cancel($item);
	}
	$return['list']=$list;
	exi($return);
}
// js 获取数据
if($_RQ['op']=='gettanmus'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 30;
	$total='';
	$fields=array('id','mid','status');
	$list=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	foreach($list as &$item){
		$member=cfc('core')->member_show($item['mid']);
		$member['nickname']=mb_substr($member['nickname'],0,4,'utf-8').'***';
		$item['nickname']=$member['nickname'];
		$member['avatar']=$member['avatar']?:'../core/resource/images/nopic.jpg';
		$item['avatar']=tomedia($member['avatar']);
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='pay'){
	$id = intval($_RQ['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=model('jy_weishop/order')->get_order($id);
	foreach($order['goods'] as $gv){
		model('jy_weishop/goods')->check_buy($gv['goodsid'],$gv['total'],$gv['optionid']);
	}
	if($order['ding_price']>0){
		$dingorder=pdo_get('jy_weishop_order_ding',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$id));
		if($dingorder['status']!=2){
			exi('该订单的保证金还没交或还未成单，不能支付余款！','error');
		}
	}
	exi($order['pay_id']);
}
if($_RQ['op']=='cancel'){
	$post=get_postdata();	
	$id=intval($post['id']);
	if($id){
		model('jy_weishop/order')->order_cancel($id);
		exi('取消成功！','','jy_weishop/order.list/');		
	}else{		
		exi('取消失败！','error');
	}

}
if($_RQ['op']=='receive'){
	$post=get_postdata();
	$id=intval($post['id']);
	model('jy_weishop/order')->order_receive($id);
	exi('收货成功！');	
}
//支付订单
if($_RQ['op']=='payaddfee'){
	$id=intval($_RQ['id']);
	$order=pdo_get('jy_weishop_order',array('id'=>$id));
	$isorder=pdo_get('jy_weishop_order_addfee',array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$order['mid'],
			'orderid'=>intval($order['id'])
			)
	);
	if($isorder['id']){
		$payid=$isorder['pay_id'];
		$order_id=$isorder['id'];
	}else{
		$order_id=$this->model('order')->createaddfee_order($order);
		$params=array(
			'id'=>$order_id,
			'title'=>'购买商品',
			'fee'=>$order['addprice'],
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_weishop/order.list/addpayresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>$success_url?:'jy_weishop/order.list/',
			'error_url'=>$error_url?:'jy_weishop/order.list/',
		);
		$payid=cfc('pay')->create_pay_order($params);
		$this->pdo_update('order_addfee',array('pay_id'=>$payid),array('id'=>$order_id));
	}
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$order_id,
		'success_url'=>$success_url,
		'error_url'=>$error_url,
	);
	exi($res);
}
if($_RQ['op']=='addpayresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_weishop/order.list/');
	}
	$order=pdo_get('jy_weishop_order_addfee',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_weishop/order.list/');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error','jy_weishop/order.list/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	$this->model('order')->order_addfeepay($order['id'],$params);
	exi('提交成功!');
}
if($_RQ['op']=='sendsubmsg'){
	$id = intval($_RQ['id']);
	//已取消支付推送，需这里推送消息
	$this->model('order')->sendsubmsg($id);
	exi('success');	
}
if($_RQ['op']=='getnew'){
	$params=array(
		'status'=>1,
		'uniacid'=>$_SESSION['uniacid'],
		'merchid'=>intval($_RQ['merchid']),
		'api_pluginname'=>$_RQ['api_pluginname']?:'',
	);
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
	}
	if($_RQ['api_id']){
		$params['api_id']=intval($_RQ['api_id']);
	}
	$order=pdo_get('jy_weishop_order',$params);
	if($order['id']){
		exi($order);
	}else{
		exi('none');
	}
}
if($_RQ['op']=='buyto'){
	$orderid=intval($_RQ['orderid']);
	$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
	$storeid=$order['storeid'];
	$merchid=$order['merchid'];
	$sendtype=$order['sendtype'];
	$is_dish=$order['is_dish'];
	$cart_list=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'orderid'=>$orderid),array('id','goodsid','total','optionid','merchid'));
	if(!$cart_list){
		exi('该订单没有商品！','error');
	}
	foreach($cart_list as &$cart){
			$goods=pdo_get('jy_weishop_goods',array('id'=>$cart['goodsid']),array('id'));
			if(!$goods){
				exi('指定商品不存在！','error');
			}
			//$this->model('goods')->check_buy($goods['id'],$cart['total'],$cart['optionid']);
	}
	$_SESSION['cart_list']=$cart_list;
	if($_SESSION['current_plugin']=='jy_weidish' || $_SESSION['current_plugin']=='jy_weishoppei' || $_SESSION['current_plugin']=='jy_weishoppeishop' || $_SESSION['current_plugin']=='jy_weishopscale'){
		if($_SESSION['current_plugin']=='jy_weishoppeishop'){
			exi('','','jy_weishoppei/create//isbuyto=1');
		}elseif($_SESSION['current_plugin']=='jy_weishopscale'){
			exi('','','jy_weishopscale/create//isbuyto=1');
		}else{
			exi('','',$_SESSION['current_plugin'].'/create//isbuyto=1');
		}
	}elseif($merchid){
		exi('','','jy_merch/order.create//isbuyto=1&storeid='.$storeid.'&merchid='.$merchid.'&sendtype='.$sendtype.'&is_dish='.$is_dish);
	}else{
		exi('','','jy_weishop/order.create//isbuyto=1&storeid='.$storeid.'&merchid='.$merchid.'&sendtype='.$sendtype.'&is_dish='.$is_dish);
	}
}
?>